package cn.com.connext.scrm.web.rest;

import cn.com.connext.scrm.service.WriteOffRecordService;
import cn.com.connext.scrm.service.dto.JsonResult;
import cn.com.connext.scrm.service.dto.WriteOffRecordDTO;
import cn.com.connext.scrm.utils.ExcelUtil;
import cn.com.connext.scrm.web.rest.vm.WriteOffRecordExcelVm;
import cn.com.connext.scrm.web.rest.vm.WriteOffRecordPageVM;
import cn.com.connext.scrm.web.rest.vm.WriteOffRecordVM;
import com.connext.common.web.rest.BaseResource;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 第三方调用接口
 */
@RestController
@RequestMapping("/api/writeoffrecord/")
public class WriteOffRecordResource extends BaseResource {
    private final Logger logger = LoggerFactory.getLogger(WriteOffRecordResource.class);

    @Autowired
    private WriteOffRecordService writeOffRecordService;

    public WriteOffRecordResource() {
        super("WriteOffRecord");
    }

    /**
     * 添加核销码记录
     */
    @ApiOperation(value = "添加核销码记录", notes = "添加核销码记录")
    @PostMapping(value = "/add")
    public JsonResult add(@RequestBody WriteOffRecordVM worm){
        return  writeOffRecordService.save(worm);
    }
    /**
     * 分页条件查询核销列表
     */
    @ApiOperation(value = "分页条件查询核销列表", notes = "分页条件查询核销列表")
    @PostMapping(value = "/findByPage")
    public JsonResult findByPage(@RequestBody WriteOffRecordPageVM worm){
        JsonResult result = new JsonResult();
      try{
            Page<WriteOffRecordDTO> page = writeOffRecordService.findByPage(worm);
            result.setData(page);
            result.setState(0);
        } catch (Exception e) { logger.error("e:{}", e);
            result.setState(-1);
            result.setMessage("查询失败");
        }
        return result;
    }

    /**
     * 导出Excel
     * @return
     */
    @PostMapping("/downloadExcel")
    public void downloadExcel(@RequestBody WriteOffRecordPageVM worm,HttpServletResponse response) {
        String sheetName = "核销记录列表";
        String fileName = "核销记录列表";
        List<WriteOffRecordExcelVm> userList = writeOffRecordService.findAll(worm);
        try {
            ExcelUtil.writeExcel(response, userList, fileName, sheetName, WriteOffRecordExcelVm.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

